﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HomeDms.Imaging
{
    public partial class ImageToolPanel : UserControl
    {
        public ImageToolPanel()
        {
            InitializeComponent();
        }

        private bool modified = false;

        public bool Modified
        {
            get
            {
                return modified;
            }
        }

        public event EventHandler ApplyTool;
        public event EventHandler CancelTool;

        protected void InvalidateImage()
        {
            pictureBox.OriginalImage = bitmap;
        }

        private Bitmap original;

        public Image Original
        {
            get
            {
                return original;
            }
        }

        private Bitmap bitmap;
        protected Bitmap Bitmap
        {
            get
            {
                return original;
            }
            set
            {
                bitmap = value;
                modified = true;
            }
        }

        protected virtual void FinalApply()
        {
        }

        private bool paramsModified = false;

        protected virtual void Changed()
        {
            paramsModified = true;
        }

        private QualityPictureBox pictureBox;

        public virtual QualityPictureBox PictureBox
        {
            get
            {                
                return pictureBox;
            }
            set
            {
                if (!(value.OriginalImage is Bitmap))
                    throw new Exception("Image must be a bitmap");
                modified = false;
                pictureBox = value;                
                bitmap = value.OriginalImage as Bitmap;
                original = bitmap.Clone() as Bitmap;
                AForge.Imaging.Image.FormatImage(ref bitmap);
                InvalidateImage();
            }
        }

        protected void Apply()
        {
            FinalApply();
            InvalidateImage();
            if (ApplyTool != null)
                ApplyTool(this, EventArgs.Empty);
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            Apply();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        protected void Cancel()
        {
            bitmap = original;
            InvalidateImage();
            if (CancelTool != null)
                CancelTool(this, EventArgs.Empty);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (paramsModified)
            {
                timer1.Enabled = false;
                Execute();
                paramsModified = false;
                timer1.Enabled = true;
            }
        }

        protected virtual void Execute()
        {
        }
    }
}
